import { login } from '@/api/user'
const userModule = {
  namespaced: true,
  state: {
    userInfo: null,
    token: null,
  },
  getters: {
    userInfo: state => state.userInfo,
    token: state => state.token,
  },
  mutations: {
    loginSuccess(state, payload) {
      state.userInfo = payload.userInfo
      state.token = payload.token
    },
    reset(state) {
      state.userInfo = null
      state.token = null
    },
  },
  actions: {
    async loginAction(context, payload) {
      const { userInfo, token } = await login(payload)
      if (userInfo) {
        context.commit('loginSuccess', { userInfo, token })
        return true
      } else {
        context.commit('reset')
        return false
      }
    },
  },
}
export default userModule
